package cn.org.controller;

import java.util.List;
/*
 * 登录控制
 */

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.org.domain.Org;
import cn.org.domain.Person;
import cn.org.domain.PersonOrgPos;
import cn.org.service.IOrgService;
import cn.org.service.IPersonService;

@Controller
public class LoginController {
	@Resource
	private IPersonService personService; 
	@Resource
	private IOrgService orgService;
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String toLogin(@RequestParam(value="username") String un, @RequestParam(value="password") String psw,HttpSession session){
		Person user=personService.getPersonByName(un);
		System.out.println(user);
		if(user!=null){
			session.setAttribute("user", user);
		}
		List<Org> list=orgService.getOrgByPerson(user);
		session.setAttribute("org", list.get(0));
		List<PersonOrgPos> adminlist=personService.selectByPerson(user);
		for(PersonOrgPos pop:adminlist){
			if(pop.getAdminOrgLevel()==1) return "index";
		}
		return "comindex";
	}
	@RequestMapping("/home")
	public String toIndex(HttpSession session){
		Person user=(Person) session.getAttribute("user");
		List<Org> list=orgService.getOrgByPerson(user);
		System.out.println("home org----->"+list.get(0));
		session.setAttribute("org", list.get(0));
		return "index";
	}
	@RequestMapping("/comhome")
	public String tocomIndex(HttpSession session){
		Person user=(Person) session.getAttribute("user");
		List<Org> list=orgService.getOrgByPerson(user);
		System.out.println("home org----->"+list.get(0));
		session.setAttribute("org", list.get(0));
		return "comindex";
	}
	@RequestMapping(value="/logout")
	public String toLogout(HttpSession session){
		Person user=(Person) session.getAttribute("user");
		System.out.println(user);
		if(user!=null){
			session.removeAttribute("user");
			session.invalidate();
		}
		return "redirect:/index.html";
	}
}
